/*
Copyright 2012 Cuong Pham

This file is part of eTrack project.

eTrack is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.

eTrack is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with eTrack. If not, see http://www.gnu.org/licenses/.
*/

/***********************************************************************************************
 *    Project Name: eTrack
 *    Project website: http://code.google.com/p/etrack/
 * *********************************************************************************************/

import com.rapplogic.xbee.api.XBeeException;
import com.rapplogic.xbee.api.XBee;
import com.rapplogic.xbee.api.XBeeResponse;
import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class BaseStation {
    private static int RUN_NUMBER = 5;
    private static int NUM_CLASS = 91;
    private static String RUN_TYPE = "XBT";
    public static int RESET = 100;
    public static int RSSI = 102;
    public static String rootTraining = "C:\\TrainingData\\Run"+RUN_NUMBER;    
        
    public static void main(String[] args) throws IOException, XBeeException, InterruptedException {
   
        try{
            boolean success = (
                  new File(rootTraining)).mkdir();
                  if (success) {
                  System.out.println("Directory: " 
                   + rootTraining + " created");
                  }
        }
        catch(Exception e){
              System.err.println("Error: " + e.getMessage());

        }

        XBee xbee = new XBee();
        int source = 0;
        int msgType = 0;
        int serial = 0;
        int rssi = 0;
        FileOutputStream out; // declare a file output object
        PrintStream p; // declare a print stream object
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Calendar cal = Calendar.getInstance();
        
        try{
            out = new FileOutputStream(rootTraining+"\\rawTraining"+RUN_TYPE+""+NUM_CLASS+".txt");

            // Connect print stream to the output stream
            p = new PrintStream( out );

        try {
            // replace with your com port and baud rate. this is the com port of my coordinator
            xbee.open("COM11", 9600);
            
            long startTime = System.currentTimeMillis();
            long endTime=startTime;
            while (endTime-startTime<60000) {
                endTime = System.currentTimeMillis();
                XBeeResponse response = xbee.getResponse();
                source = response.getProcessedPacketBytes()[response.getLength().get16BitValue()-4];
                msgType = response.getProcessedPacketBytes()[response.getLength().get16BitValue()-1];
                serial = response.getProcessedPacketBytes()[response.getLength().get16BitValue()];
                rssi = response.getProcessedPacketBytes()[response.getLength().get16BitValue()+1];
                if (msgType==RSSI){
                    //get current date time with Calendar()
                    cal = Calendar.getInstance();
                    System.out.println("Source = "+source+" Serial = "+serial+" RSSI = "+rssi+" Time ="+dateFormat.format(cal.getTime()));
                    p.println(source+";"+serial+";"+rssi+";"+dateFormat.format(cal.getTime()));
                }
                else if (msgType == RESET ){
                    System.out.println();
                    p.println();
                }
                //System.out.println(msgType+" received response " + response);
                //System.out.print("received response " + response.getProcessedPacketBytes()[response.getLength().get16BitValue()+1]+"----");
                //System.out.println("received response " + response.getProcessedPacketBytes()[response.getLength().get16BitValue()]);
            }
        } finally {
            xbee.close();
        }

        }
        catch (Exception e)
        {
              System.err.println("Error: " + e.getMessage());
        }
  
    }

}

